package org.wj.problem;

public class FindMedianSortedArrays {

	public static void main(String[] args) {
		int[] nums1 = {1, 2};
		int[] nums2 = {3, 4};
		double medianSortedArrays = findMedianSortedArrays(nums1, nums2);
		System.out.println(medianSortedArrays);
	}

	public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

		int[] mergeArrays = new int[nums1.length + nums2.length];
		int n = 0;
		int m = 0;
		for (int i = 0; i < mergeArrays.length; i++) {

			if (n >= nums1.length) {
				mergeArrays[i] = nums2[m];
				m++;
				continue;
			}
			if (m >= nums2.length) {
				mergeArrays[i] = nums1[n];
				n++;
				continue;
			}

			if (nums1[n] > nums2[m]) {
				mergeArrays[i] = nums2[m];
				m++;
			} else {
				mergeArrays[i] = nums1[n];
				n++;
			}
		}
		if (mergeArrays.length <= 1) {
			return mergeArrays[0];
		}
		if (mergeArrays.length % 2 == 0) {
			int mid = mergeArrays.length / 2;
			return (mergeArrays[mid] + mergeArrays[mid - 1]) / 2.0;
		} else {
			return mergeArrays[mergeArrays.length / 2];
		}

	}

}
